from datetime import datetime
from sqlalchemy import func,text
from applications.models import *
def get_djbh(type,data):
    result=''
    if type=='入库退货_yl_yp':
        #'CK_TH_2020_10_0006'
        current_date = datetime.strptime(data, '%Y-%m-%d').strftime('%y%m')

        item = StoreRawYPReturnSupplierModel.query.filter(
            func.date_format(StoreRawYPReturnSupplierModel.date_time, '%y%m') == current_date

        ).order_by(StoreRawYPReturnSupplierModel.return_dh.desc()).first()
        if item:
            result = str(item.return_dh[:-4]) + str(int(item.return_dh[-4:]) + 1).zfill(4)
        else:
            result ='CK_TH_'+'_'.join(data.split('-')[:2])+'_0001'
    elif type=='仓库报损_yl_yp':
        #'CK_TH_2020_10_0006'
        current_date = datetime.strptime(data, '%Y-%m-%d').strftime('%y%m')

        item = StoreRawYPLossModel.query.filter(
            func.date_format(StoreRawYPLossModel.date_time, '%y%m') == current_date

        ).order_by(StoreRawYPLossModel.loss_dh.desc()).first()
        if item:
            result = str(item.loss_dh[:-4]) + str(int(item.loss_dh[-4:]) + 1).zfill(4)
        else:
            result ='CK_BS_'+'_'.join(data.split('-')[:2])+'_0001'

    elif type=='仓库报溢_yl_yp':
        #'CK_TH_2020_10_0006'
        current_date = datetime.strptime(data, '%Y-%m-%d').strftime('%y%m')

        item = StoreRawYPExcessModel.query.filter(
            func.date_format(StoreRawYPExcessModel.date_time, '%y%m') == current_date

        ).order_by(StoreRawYPExcessModel.excess_dh.desc()).first()
        if item:
            result = str(item.excess_dh[:-4]) + str(int(item.excess_dh[-4:]) + 1).zfill(4)
        else:
            result ='CK_BY_'+'_'.join(data.split('-')[:2])+'_0001'
    elif type=='请验管理_yl_yp':
        #'CK_TH_2020_10_0006'
        current_date = datetime.strptime(data, '%Y-%m-%d').strftime('%y%m')

        item = VerifyRawYPModel.query.filter(
            func.date_format(VerifyRawYPModel.date_time, '%y%m') == current_date

        ).order_by(VerifyRawYPModel.verify_dh.desc()).first()
        if item:
            result = str(item.verify_dh[:-4]) + str(int(item.verify_dh[-4:]) + 1).zfill(4)
        else:
            result ='ZL_VF_'+'_'.join(data.split('-')[:2])+'_0001'
    elif type=='请验管理_yl_yp_sample':
        #'CK_TH_2020_10_0006'
        current_date = datetime.strptime(data, '%Y-%m-%d').strftime('%y%m')

        item = VerifyRawYPModelSample.query.filter(
            func.date_format(VerifyRawYPModelSample.date_time, '%y%m') == current_date

        ).order_by(VerifyRawYPModelSample.verify_dh.desc()).first()
        if item:
            result = str(item.verify_dh[:-4]) + str(int(item.verify_dh[-4:]) + 1).zfill(4)
        else:
            result ='ZL_VF_SP_'+'_'.join(data.split('-')[:2])+'_0001'
    elif type=='取样管理_yl_yp':
        #'CK_TH_2020_10_0006'
        current_date = datetime.strptime(data, '%Y-%m-%d').strftime('%y%m')

        item = SampleRawYPModel.query.filter(
            func.date_format(SampleRawYPModel.date_time, '%y%m') == current_date

        ).order_by(SampleRawYPModel.sample_dh.desc()).first()
        if item:
            result = str(item.sample_dh[:-4]) + str(int(item.sample_dh[-4:]) + 1).zfill(4)
        else:
            result ='ZL_SP_'+'_'.join(data.split('-')[:2])+'_0001'
    elif type=='取样管理_yl_yp_sample':
        #'CK_TH_2020_10_0006'
        current_date = datetime.strptime(data, '%Y-%m-%d').strftime('%y%m')

        item = SampleRawYPModelSample.query.filter(
            func.date_format(SampleRawYPModelSample.date_time, '%y%m') == current_date

        ).order_by(SampleRawYPModelSample.sample_dh.desc()).first()
        if item:
            result = str(item.sample_dh[:-4]) + str(int(item.sample_dh[-4:]) + 1).zfill(4)
        else:
            result ='ZL_SP_SP_'+'_'.join(data.split('-')[:2])+'_0001'
    print(result)
    return result

def get_isSameMonth(data:str,djbh:str):
    '''判断月份是否一致
    data:2024-10-06
    djbh:CK_TH_2020_10_0006

    '''
    result=False
    current_date = datetime.strptime(data, '%Y-%m-%d').strftime('%Y%m')
    djbh_date=''.join(djbh.split('_')[2:4])
    if current_date==djbh_date:
        return True

    return result
